﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KamRemakeRemake.Common.BaseClasses
{
    public class BaseEnumList<T> : List<T> where T : struct
    {
        /// <summary>
        /// Contains a list of enums. for example tiles, actions or unitstatuses
        /// </summary>
        /// <param name="statuses"></param>
        protected BaseEnumList(params T[] statuses)
        {
            AddRange(statuses);

#if DEBUG
            CheckIntegrity();
#endif
        }

        public virtual T[] GetKnownValues()
        {
            return new T[] {};
        }

        public void CheckIntegrity()
        {
            T[] known = GetKnownValues();

            Array all = Enum.GetValues(typeof (T));
            foreach (T item in all)
            {
                if (!known.Contains(item))
                {
                    throw new Exception(
                        string.Format(
                            "{0}es have been added or removed. Reevaluate all status selections in {0}List",
                            typeof (T).Name));
                }
            }
        }

        public string AsSqlParam()
        {
            return string.Join(",", AsStrings().ToArray());
        }

        public List<string> AsStrings()
        {
            List<string> result = new List<string>();
            foreach (T item in this)
            {
                int intValue = Convert.ToInt32(item);
                result.Add(intValue.ToString());
            }
            return result;
        }
    }
}
